/*
	----------------------------------------------------------
*/
var wp;
var editor;

//静态-类型下拉选框
var sc = [
	{type:0, data:["","0","1"],str:["","有效","无效"]},
	{type:1, data:["","0","1","2"],str:["","个人","单位","开发商"]},
];
//静态-按钮区
var button_group = {};

$(function () {
	//自定义对象
	wp = {
		fn : new Object()
	};
	//自定义方法
	wp.fn.toSource = function(type,num,sc){
		var d = sc[type];
		for(var i in d.data){
			var r = d.data[i];
			if(xyd.fn.geteTypeof(r)==xyd.fn.geteTypeof(num)){
				if(r==num){return d.str[i];}
			}else{
				return undefined; // 类型错误
			}
		}
		return undefined; // 数据错误
	};

	//0.配置相关参数
	var tableId = "#tb_departments",		//表格ID
		selectTrue = false,					//是否单选,true为单选，false为多选
		ajaxUrl = xyd.api.path+'/notify/list',
		ajaxType = 'get',					//ajax方式,get,post
		ajaxTableColumns = [{				//表格头信息
			checkbox: true
		},{
			field: 'contType', title: '类型'
		},{
			field: 'contTitle', title: '标题'
		},{
			field: 'contAbstract', title: '摘要'
		},{
			field: 'contDesc', title: '描述'
			/*,escape:true*/
		},{
			field: 'logoPath', title: '图片地址'
		},{
			field: 'seqNo', title: '显示顺序'
		},{
			field: 'isEnable', title: '是否有效'
		},{
			field: 'crtDatetime', title: '创建时间'
		},{
			field: 'uptDatetime', title: '修改时间'
		},{
			field: 'operId', title: '操作人'
		},{
			field: 'edit', title: '操作'
		}]
		; 

	//1.初始化Table
	var oTable = new TableInit(tableId,selectTrue);

	//2.配置查询参数
	oTable.queryParams = function (params) {
		var temp = {
			// 查询条件，传实体类属性
			contType : $("#txt_search_1").val(),
			contTitle : $("#txt_search_2").val(),
			isEnable : $("#txt_search_3").val(),

			// pageSize,pageNumber不允许更改，但必须存在，分页用
			pageSize: params.pageSize,
			pageNumber:params.pageNumber,
			// 时间戳（可不传）
			timestamp: new Date().getTime()
		};
		return temp;
	};
	
	//3.提交查询
	oTable.ajaxSubmit({
		url : ajaxUrl,
		type : ajaxType,
		columns : ajaxTableColumns,
		uniqueId : "id"  //uuid -> id
	});
	
	//数据加载完毕后增加内部按钮
	oTable.onLoadSuccess(function(){
		var data = oTable.getData();
		for(var i=0;i<data.length;i++){
			oTable.updateRow(i,{
				"edit":"<div class=\"hidden-sm hidden-xs btn-group\"> \
				<button onclick=\"button_group._search('"+data[i].id+"')\" class=\"btn btn-xs btn-success\">\
					<span aria-hidden=\"true\" class=\"glyphicon glyphicon-search\"></span>\
				</button>\
				<button onclick=\"button_group._edit('"+data[i].id+"')\" class=\"btn btn-xs btn-info\">\
					<i class=\"ace-icon fa fa-pencil bigger-120\"></i>\
				</button>\
				<button onclick=\"button_group._removeId('"+data[i].id+"')\" class=\"btn btn-xs btn-danger btn-user-delete\">\
					<i class=\"ace-icon fa fa-trash-o bigger-120\"></i>\
				</button>\
			</div>"
			});
			// 列数据转换
			oTable.updateRow(i,{"isEnable":wp.fn.toSource(0,data[i].isEnable+"",sc)});
			oTable.updateRow(i,{"contType":wp.fn.toSource(1,data[i].contType+"",sc)});
		}
	});
	
	/*
		----------------------------------------------------------
		按钮功能区
		----------------------------------------------------------
	*/
	
	var toolbar = $("#toolbar");
	var btn_add = toolbar.find("#btn_add");
	var btn_removeSel = toolbar.find("#btn_delete");
	// var btn_httpDown = toolbar.find("#btn_httpDown");
	// var btn_httpUpload = toolbar.find("#btn_httpUpload");
	
	//按钮-查询数据
	$("#btn_query").click(function(){
		oTable.search();
	});
	
	//按钮-增加本地数据
	btn_add.click(function(){
		$.get(xyd.pageBean.jm+'notify/add.html', {}, function(str){
			// console.info(xyd.sys.kindeditor.js.min);
			$.getScript(xyd.sys.kindeditor.js.min, function() {
				// 弹出窗体
				layer.open({
					type: 1,
					area: ['700px', 'auto'],
					title: '新增',
					content: str, //注意，如果str是object，那么需要字符拼接。
					btn: ['新增', '取消'],
					yes: function (i) {

						layer.confirm('您确认保存这条数据吗?', {title:'提示'}, function(index){
							var form = $("#form_add");
							var var1 = form.find("select[name='select_1']");
							var var2 = form.find("input[name='input_1']");
							var var3 = form.find("input[name='input_2']");
							var var5 = form.find("input[name='input_3']");
							var var6 = form.find("input[name='input_4']");
							var var7 = form.find("select[name='select_isEnable']");
							var dataList = {
								contType : var1.val(),
								contTitle : var2.val(),
								contAbstract : var3.val(),
								contDesc : editor.html(),
								logoPath : var5.val(),
								seqNo : var6.val(),
								isEnable : var7.val()
							};

							$.ajax({
								url: xyd.api.path+'/notify/add',
								data: dataList,
								type: 'POST',
								dataType: 'JSON'
							}).done(function ( data ) {
								layer.msg(data.statusCode);
								if(data.success){
									oTable.search();
									layer.close(i);
								}
							});
						})
					}
				});

				// 配置富文本编辑器(kindeditor)
				KindEditor.basePath = xyd.sys.kindeditor.path + "/";
				editor = KindEditor.create('textarea[name="content"]',{
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link'], zIndex :999999999
				});
			});

		});
		
	});

	//按钮-删除选中数据
	btn_removeSel.click(function(){
		var d = oTable.selectId();
		if(d.length<1){
			layer.msg('请选择数据行');
			return;
		}
		layer.confirm('您确认删除选中的全部数据吗?删除后不可恢复！', {title:'提示'}, function(index){
			var ids = "";
			for(var i in d){
				ids += d[i].id+",";
			}
			ids = ids.substr(0,ids.length-1);
			// 表单提交
			$.ajax({
				url: xyd.api.path+'/notify/deletes/'+ids,
				type: 'POST',
				dataType: 'JSON'
			}).done(function ( data ) {
				layer.msg(data.statusCode);
				if(data.success){
					layer.close(i);
					oTable.search();
				}
			});
			// 表单提交 结束//
		});
	});

	
	//内部按钮-查看
	button_group._search = function(id){
		var data = oTable.getId(id);
		$.get(xyd.pageBean.jm+'notify/add.html', {}, function(str){
			/*加载富文本编辑器(kindeditor)依赖*/
			$.getScript(xyd.sys.kindeditor.js.min, function() {
				// 弹出窗体
				layer.open({
					type: 1,
					area: ['700px', 'auto'],
					title: '查看详情',
					content: str,
					btn: ['关闭']
				});
				// 配置富文本编辑器(kindeditor)
				KindEditor.basePath = xyd.sys.kindeditor.path + "/";
				editor = KindEditor.create('textarea[name="content"]',{
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link'], zIndex :999999999
				});

				/* 数据回显*/
				var form = $("#form_add");
				var var1 = form.find("select[name='select_1']");
				var var2 = form.find("input[name='input_1']");
				var var3 = form.find("input[name='input_2']");
				var var5 = form.find("input[name='input_3']");
				var var6 = form.find("input[name='input_4']");
				var var7 = form.find("select[name='select_isEnable']");

				form.find("input").val("").attr("disabled","disabled");
				var1.val("").attr("disabled","disabled");
				var1.find("option").each(function(){
					if($(this).text()==data.contType){
						$(this).attr("selected",true);
					}
				});
				var2.val(data.contTitle);
				var3.val(data.contAbstract);
				// var4.val(data.contDesc);
				editor.html(data.contDesc);
				var5.val(data.logoPath);
				var6.val(data.seqNo);
				var7.val("").attr("disabled","disabled");
				var7.find("option").each(function(){
					if($(this).text()==data.isEnable){
						$(this).attr("selected",true);
					}
				});
				/* 数据回显 结束*/
			}); /*加载富文本编辑器(kindeditor)依赖 结束*/
		});
	}
	//内部按钮-编辑
	button_group._edit = function(id){
		var data = oTable.getId(id);
		$.get(xyd.pageBean.jm+'notify/add.html', {}, function(str){
			$.getScript(xyd.sys.kindeditor.js.min, function() {
				/* 弹出窗体*/
				layer.open({
					type: 1,
					area: ['700px', 'auto'],
					title: '编辑用户',
					content: str, //注意，如果str是object，那么需要字符拼接。
					btn: ['编辑保存', '取消'],
					yes:function(i){
						layer.confirm('您确认保存这条数据吗?', {title:'提示',btn: ['YES', 'NO']}, function(index){
							var dataList = {
								id : data.id,
								contType : var1.val(),
								contTitle : var2.val(),
								contAbstract : var3.val(),
								contDesc : editor.html(),
								logoPath : var5.val(),
								seqNo : var6.val(),
								isEnable : var7.val()
							};
							/* 表单提交*/
							$.ajax({
								url: xyd.api.path+'/notify/update',
								data: dataList,
								type: 'POST',
								dataType: 'JSON'
							}).done(function ( data ) {
								layer.msg(data.statusCode);
								if(data.success){
									layer.close(i);
									oTable.search();
								}
							});/* 表单提交 结束*/
						});
					}
				});
				// 配置富文本编辑器(kindeditor)
				KindEditor.basePath = xyd.sys.kindeditor.path + "/";
				editor = KindEditor.create('textarea[name="content"]',{
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link'], zIndex :999999999
				});

				/* 数据回显*/
				var form = $("#form_add");
				var var1 = form.find("select[name='select_1']");
				var var2 = form.find("input[name='input_1']");
				var var3 = form.find("input[name='input_2']");
				var var5 = form.find("input[name='input_3']");
				var var6 = form.find("input[name='input_4']");
				var var7 = form.find("select[name='select_isEnable']");

				var1.find("option").each(function(){
					if($(this).text()==data.contType){
						$(this).attr("selected",true);
					}
				});
				var2.val(data.contTitle);
				var3.val(data.contAbstract);
				editor.html(data.contDesc);
				var5.val(data.logoPath);
				var6.val(data.seqNo);
				var7.find("option").each(function(){
					if($(this).text()==data.isEnable){
						$(this).attr("selected",true);
					}
				});
				/* 数据回显 结束*/
			}); /* 加载富文本编辑器(kindeditor)依赖 结束*/
		});
	}
	
	//内部按钮-删除
	button_group._removeId = function(id){
		if(!id){
			layer.msg("参数错误");
			return;
		}
		layer.confirm('您确认删除该条数据吗? 删除后不可恢复！', {title:'提示',btn: ['YES', 'NO']}, function(index){
			oTable.removeId(id);
			// 表单提交
			$.ajax({
				url: xyd.api.path+'/notify/delete/'+id,
				type: 'POST',
				dataType: 'JSON'
			}).done(function ( data ) {
				layer.msg(data.statusCode);
				if(data.success){
					layer.close(i);
					oTable.search();
				}
			});
			// 表单提交 结束//
		});	
		
	}

});
